﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Controller
{
    public class StaffController
    {
        public IList<Staff> GetAllStaff()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Staff>().ToList();
            }
        }

        public IList<Member> GetAllMember()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Member>().ToList();
            }
        }

        public String GetStaffName(Staff staff)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Staff>().ToList()
                    .Where(s => s.Name == staff.Name)
                    .Select(s => s)
                    .Single()
                    .Name;
            }
        }

        public Staff GetStaffByUsername(string username)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Staff>().ToList()
                    .Where(s => s.Username == username)
                    .Select(s => s)
                    .Single();
            }
        }

        public int AddStaff(Staff staff)
        {
            try
            {
                GetAllStaff()
                    .Where(s => s.Username == staff.Username)
                    .Select(s => s)
                    .Single();

                return 0;
            }
            catch (Exception)
            {
                try
                {
                    GetAllStaff()
                        .Where(s => s.Email == staff.Email)
                        .Select(s => s)
                        .Single();

                    return -1;
                }
                catch (Exception)
                {
                    try
                    {
                        GetAllMember()
                            .Where(m => m.Username == staff.Username)
                            .Select(m => m)
                            .Single();

                        return 0;
                    }
                    catch (Exception)
                    {
                        try
                        {
                            GetAllMember()
                                .Where(m => m.Email == staff.Email)
                                .Select(m => m)
                                .Single();

                            return -1;
                        }
                        catch (Exception)
                        {
                            using (var session = NHibernateHelper.OpenSession())
                            {
                                using (var transaction = session.BeginTransaction())
                                {
                                    session.Save(staff);
                                    transaction.Commit();
                                }
                            }

                            return 1;
                        }
                    }
                }
            }
        }

        public int EditStaff(Staff newStaff, String oldEmail)
        {
            try
            {
                var checkStaff = GetAllStaff()
                    .Where(s => s.Email == newStaff.Email)
                    .Select(s => s)
                    .Single();

                if (checkStaff.Email == oldEmail)
                {
                    using (var session = NHibernateHelper.OpenSession())
                    {
                        using (var transaction = session.BeginTransaction())
                        {
                            session.SaveOrUpdate(newStaff);
                            transaction.Commit();
                        }
                    }

                    return 1;
                }

                return 0;
            }
            catch (Exception)
            {
                try
                {
                    GetAllMember()
                        .Where(m => m.Email == newStaff.Email)
                        .Select(m => m)
                        .Single();

                    return 0;
                }
                catch (Exception)
                {
                    using (var session = NHibernateHelper.OpenSession())
                    {
                        using (var transaction = session.BeginTransaction())
                        {
                            session.SaveOrUpdate(newStaff);
                            transaction.Commit();
                        }
                    }

                    return 1;
                }
            }
        }

        public void DeleteStaff(Staff staff)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(staff);
                    transaction.Commit();
                }
            }
        }
    }
}
